package server;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.Unreferenced;
import java.rmi.server.UnicastRemoteObject;
import java.io.Serializable;

public class FirewallSessionImpl 
extends UnicastRemoteObject
implements Remote, FirewallSession, Unreferenced, Serializable {
	private static final long serialVersionUID = 9108713383945649037L;
	private FirewallManagerImpl m_manager;
	private boolean m_isReadWrite;
	private boolean m_isExpert;
	
	public FirewallSessionImpl(
			FirewallManagerImpl manager, 
			boolean isExpert,
			boolean isReadWrite) throws RemoteException
	{
		this.m_manager = manager;
		this.m_isExpert = isExpert;
		this.m_isReadWrite = isReadWrite;
	}

	public boolean isReadWrite() throws RemoteException
	{
		return m_isReadWrite;
	}

	public boolean isExpert() throws RemoteException
	{
		return m_isExpert;
	}

	public void unreferenced()
	{
		if (m_isReadWrite) {
			this.m_manager.stopReadWriteSession();
		}
	}

	public void turnOn() throws RemoteException, FirewallException, ReadWriteException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		m_manager.comm.turnOn();
	}
	
	public void turnOff() throws RemoteException, FirewallException, ReadWriteException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		m_manager.comm.turnOff();
	}
	
	public boolean isLogEnabled() throws RemoteException
	{
		return m_manager.comm.isLogEnabled();
	}
	
	public void enableLog() throws RemoteException, FirewallException, ReadWriteException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		m_manager.comm.enableLog();
	}
	
	public void disableLog() throws RemoteException, FirewallException, ReadWriteException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		m_manager.comm.disableLog();
	}
	
	public void addRule(RulesTableType dir, String ruleset, Rule rule) throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		if (!m_isExpert)
			throw new NotExpertException();
		m_manager.comm.addRule(dir, ruleset, rule, true);
	}
	
	public void deleteRule(RulesTableType dir, String ruleset, int index) throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		if (!m_isExpert)
			throw new NotExpertException();
		m_manager.comm.deleteRule(dir, ruleset, index);
	}
	
	public Rule[] getRules(RulesTableType dir, String ruleset) throws RemoteException, FirewallException
	{
		return m_manager.comm.getRules(dir, ruleset);
	}
	
	public void addProtocol(Protocol protocol) throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		if (!m_isExpert)
			throw new NotExpertException();
		m_manager.comm.addProtocol(protocol);	
	}
	
	public void deleteProtocol(String name) throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		if (!m_isExpert)
			throw new NotExpertException();
		m_manager.comm.deleteProtocol(name);
	}
	
	public void updateProtocol(Protocol protocol) throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		if (!m_isExpert)
			throw new NotExpertException();
		m_manager.comm.updateProtocol(protocol);
	}
	
	public Protocol[] getProtocols() throws RemoteException, FirewallException
	{
		return m_manager.comm.getProtocols();
	}
	
	public void createRuleSet(String name) throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		if (!m_isExpert)
			throw new NotExpertException();
		m_manager.comm.createRuleSet(name);
	}
	
	public void deleteRuleSet(String name) throws RemoteException, FirewallException, ReadWriteException, NotExpertException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		if (!m_isExpert)
			throw new NotExpertException();
		m_manager.comm.deleteProtocol(name);
	}
	
	public String[] getRuleSets() throws RemoteException, FirewallException
	{
		return m_manager.comm.getRuleSets();
	}
	
	public void setRuleSet(String name) throws RemoteException, FirewallException, ReadWriteException
	{
		if (!m_isReadWrite)
			throw new ReadWriteException();
		m_manager.comm.setRuleSet(name);
	}
	
	public String getCurrentRuleSet() throws RemoteException
	{
		return m_manager.comm.getCurrentRuleSet();
	}
	
	public Log[] getLogs(int days) throws RemoteException, FirewallException
	{
		return m_manager.comm.getLogs(days);
	}
	
	public Log[] getLogs(int days, String address, Integer port, Log.directOpt dir) throws RemoteException, FirewallException
	{
		return null;
	}
	
	public Log[] getLogs(int days, String protocol) throws RemoteException, FirewallException
	{
		return null;
	}
	
	public GlobalLog[] getGlobalLogs(int days) throws RemoteException, FirewallException
	{
		return m_manager.comm.getGlobalLogs(days);
	}
}
